package com.huixue.cn.ztej.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huixue.cn.ztej.bean.Results;
import com.huixue.cn.ztej.bean.TGoods;
import com.huixue.cn.ztej.service.TGoodsService;
import com.huixue.cn.ztej.utils.SystemContents.API_RETURN_STATUS;
import com.huixue.cn.ztej.utils.framwork.PageParameter;


@Controller
public class TGoodsController extends BaseController {

	@Autowired(required = true)
	private TGoodsService tGoodsService;
	
	@RequestMapping(value = "/sys/goods/goods_list")
	public String list(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = PageParameter.DEFAULT_PAGE_SIZE+"") Integer rows,
			TGoods searchForm,Model model,HttpServletRequest request) {
		Map<String,Object> searchParams = new HashMap<String,Object>();
		if(searchForm.getGoodType()!=null){
			searchParams.put("goodType", searchForm.getGoodType());
		}
		if(searchForm.getSubjectId()!=null){
			searchParams.put("subjectId", searchForm.getSubjectId());
		}
		
		Results results = tGoodsService.searchByPage(searchParams,page,rows);
		model.addAttribute("res", results);
		model.addAttribute("searchForm", searchForm);
		return "/goods/goods_list";
	}
	
	/**
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/sys/goods/toAdd")
	public String toAdd(Model model) {
		return "/goods/goods_add";
	}
	
	/**
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/sys/goods/toUpdate/{id}")
	public String toUpate(@PathVariable ("id")Integer id,Model model,HttpServletRequest request) {
		TGoods info = tGoodsService.getInfo(id);
		model.addAttribute("goods", info);
		return "/goods/goods_update";
	}
	
	
	/**
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/sys/goods/udpate",produces="application/json;charset=UTF-8",method=RequestMethod.POST)
	public Results udpate(@RequestBody TGoods  info,Model model,HttpServletRequest request) {
		tGoodsService.update(info);
		Results results = new Results(API_RETURN_STATUS.NORMAL.value(), "修改成功！");
		return results;
	}

	@ResponseBody
	@RequestMapping(value = "/sys/goods/add",produces="application/json;charset=UTF-8",method=RequestMethod.POST)
	public Results add(@RequestBody TGoods info,HttpServletRequest request) {
		tGoodsService.save(info);
		Results results = new Results(API_RETURN_STATUS.NORMAL.value(), "保存成功！");
		return results;
	}
	
	@ResponseBody
	@RequestMapping(value = "/sys/goods/save",produces="application/json;charset=UTF-8",method=RequestMethod.POST)
	public Results save(@RequestBody TGoods info,HttpServletRequest request) {
		TGoods currentInfo = tGoodsService.getInfo(info.getGoodId());
		tGoodsService.update(currentInfo);
		Results results = new Results(API_RETURN_STATUS.NORMAL.value(), "保存成功！");
		return results;
	}
}
